package com.csdj.test.controller;


import com.csdj.test.clients.UserClient;
import com.csdj.test.entity.Article;
import com.csdj.test.entity.ArticleAndTUsers;
import com.csdj.test.entity.TUsers;
import com.csdj.test.service.ArticleService;
import com.csdj.test.service.RedisService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author 徐守志
 * @since 2022-03-08
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    ArticleService articleService;

    @Resource
    UserClient userClient;

    @Resource
    RedisService redisService;


    @GetMapping("/find/{id}")
    ArticleAndTUsers find(@PathVariable("id") int id){
        //连表Article、TUsers
        ArticleAndTUsers articleAndTUsers = new ArticleAndTUsers();


        //根据id查询到实体表Article，先从缓存中查
        Article article=redisService.getObject(id+"article",Article.class);
        if(article==null){
            //redis中没有
            article=articleService.find(id);

            //存入redis,半小时有效
            redisService.setObject(article.getId()+"article",article,1800);
        }

        //调用user服务，根据id获取到TUsers表信息
        TUsers tUsers = userClient.find(article.getUserId());


        //存入
        articleAndTUsers.setArticle(article);
        articleAndTUsers.settUsers(tUsers);
        return articleAndTUsers;
    }

    @GetMapping("/delete/{id}")
    int delete(@PathVariable("id") int id){
        return articleService.delete(id);
    }


}
